package com.doophan.duuchin.service.impl;

import com.doophan.duuchin.dto.QueryDTO;
import com.doophan.duuchin.model.Article;
import com.doophan.duuchin.model.Song;
import com.doophan.duuchin.model.User;
import com.doophan.duuchin.model.Video;
import com.doophan.duuchin.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class RecommendServiceImpl implements RecommendService {
    @Resource
    private ArticleService articleService;
    @Resource
    private SongService songService;
    @Resource
    private UserService userService;
    @Resource
    private VideoService videoService;

    private final Random random = new Random();
    private static String[] MODELS = {"user", "song", "article", "video"};

    @Override
    public Object getList(QueryDTO queryDTO) {
        List<User> users = userService.getList(queryDTO).getData();
        List<Song> songs = songService.getList(queryDTO).getData();
        List<Video> videos = videoService.getList(queryDTO).getData();
        List<Article> articles = articleService.getList(queryDTO).getData();

        String[] randModels = getRandModels(queryDTO.getLimit(), MODELS);

        List result = new ArrayList();
        for (int i = 0; i < randModels.length; i++) {
            String value = randModels[i];
            Map<String, Object> map = new HashMap<>();
            switch (value) {
                case "user":
                    map.put("userEntity", users.size() > i ?  users.get(i) : null);
                    break;
                case "song":
                    map.put("songEntity", songs.size() > i ?  songs.get(i) : null);
                    break;
                case "article":
                    map.put("articleEntity", articles.size() > i ?  articles.get(i) : null);
                    break;
                case "video":
                    map.put("videoEntity", videos.size() > i ?  videos.get(i) : null);
                    break;
                 default:
            }
            result.add(map);
        }
        return result;
    }

    private String[] getRandModels(int limit, String[] models) {
        String[] randModels = new String[limit];
        for (int i = 0; i < limit; i++) {
            int randKey =  random.nextInt(models.length - 1);
            randModels[i] = models[randKey];
        }
        return randModels;
    }
}
